﻿
using Elsa.Workflows.Management.Entities;
using Elsa.Workflows.Runtime.Messages;
using T.FAS.Runtime.MF.Entity;

namespace T.FAS.Runtime.MF.Service
{
    public class EngineExecuteService
    {
        public ElsaExecutionResult ExecuteWorkflow(string definitionId, IDictionary<string, object> input, Dictionary<string, string> extraParams=null)
        {
            var engine = ElsaEngine.GetInstanceAsync().GetAwaiter().GetResult();
            return engine.ExecuteAsync(definitionId, input, extraParams).GetAwaiter().GetResult();
        }

        public RunWorkflowInstanceResponse StartWorkflow(string definitionId, IDictionary<string, object> input)
        {
            var engine = ElsaEngine.GetInstanceAsync().GetAwaiter().GetResult();
            return engine.StartNewWorkflowInstanceAsync(definitionId, input).GetAwaiter().GetResult();
        }

        public List<WorkflowDefinition> PublishWorkflow(List<Microflow> workflowDef)
        {
            var result = new List<WorkflowDefinition>();
            var engine = ElsaEngine.GetInstanceAsync().GetAwaiter().GetResult();
            foreach (Microflow microflow in workflowDef)
            {
                var publishResult = engine.PublishAsync(microflow).GetAwaiter().GetResult();
                result.Add(publishResult);
            }
            return result;
        }

        public List<WorkflowInstanceDetail> GetExecuteDetail(string instanceId)
        {
            var engine = ElsaEngine.GetInstanceAsync().GetAwaiter().GetResult();

            return engine.GetExecuteDetailNew(instanceId);
        }
    }
}
